﻿using GameData.Domains.Mod;
using System;
using System.Collections.Generic;
using System.Reflection;
using TaiwuModFramework.Backend.ConfigItemReader;
using TaiwuModFramework.Common.Abstract;
using TaiwuModFramework.Common.Base;

namespace TaiwuModFramework.Backend {
    public class FwDomain : AFwDomain {
        public static ModInfo ModInfo { get; internal set; }

        private FwDomain() { }
        

        internal static void Init(string idStr) {
            if(ModIdStr != null) return;    // 已经初始化过了
            ModIdStr = idStr;
            ModInfo = GetModInfo(idStr);
            // 配置日志工具
            UseLog(new BaseLogger("FwBackend", ALogger.LEVEL.INFO, BaseLogWriter.GetLogWriter("./Mod/TaiwuModFramework/Logs/Backend.log")));
            // 配置获取配置项值的委托
            RegConfigItemReader(new IntReader(), new BoolReader(), new FloatReader(), new StringReader());
            MsgClient = new BaseMsgClient(ShareMemoryDefaultPath, false);
            ReqClient = new BaseReqClient();
        }




        /// <summary>
        /// 获取mod信息
        /// </summary>
        /// <param name="modIdStr"></param>
        /// <returns></returns>
        public static ModInfo GetModInfo(string modIdStr) {
            Dictionary<string, ModInfo> dict = (Dictionary<string, ModInfo>)(typeof(ModDomain).GetField("ModInfoDict", (BindingFlags)(-1)).GetValue(null));
            dict.TryGetValue(modIdStr, out ModInfo modInfo);
            return modInfo;
        }


        public static void FrontendInfo(string msg) {
            try {
                ReqClient.Req(ModIdStr, "LogInfo")
                .SetArgs(msg)
                .SendNotify();
            } catch(Exception e) {
                Log.Error(e);
            }
        }
    }
}
